package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IQsgStoreService;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.QsgStore;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.DistanceUtils;
import com.zscat.mallplus.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 门店管理Controller
 */
@RestController
@Api(tags = "QsgStoreController", description = "门店管理")
@RequestMapping("/api/qsg")
public class QsgStoreController {

    @Autowired
    private IQsgStoreService storeService;
    @Autowired
    private IDmsOrderService orderService;

    @IgnoreAuth
    @GetMapping(value = "/store/getStoreById")
    @ApiOperation(value = "根据id查询套餐")
    public Object getStoreById(Long storeId) {
        try {
            return new CommonResult().success(storeService.getById(storeId));
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @GetMapping(value = "/store/list")
    @ApiOperation(value = "门店列表")
    public Object list(@RequestParam(required = false) String storeName,
                       @RequestParam(required = false)String longitude,
                       @RequestParam(required = false)String latitude,
                       @RequestParam(required = false)String city) {
        try {
            QueryWrapper qw = new QueryWrapper();
            if (StringUtils.isNotBlank(storeName)){
                qw.like("name",storeName.trim());
            }
            if (StringUtils.isNotBlank(city)&&!city.equals("全部")){
                qw.like("address_lat",city);
            }
            List<QsgStore> stores = storeService.list(qw);
            Double fromLongitude = null;
            Double fromLatitude = null;
            if (StringUtils.isNotBlank(longitude)){
                fromLongitude = Double.valueOf(longitude);
                fromLatitude = Double.valueOf(latitude);
                if (null!=stores&&stores.size()>0){
                    for (QsgStore store : stores){
                        BigDecimal distance = DistanceUtils.getDistance(fromLongitude,fromLatitude,Double.valueOf(store.getLongitude()),Double.valueOf(store.getLatitude()),2);
                        store.setDistance(distance.toString());
                        store.setRealdistance(distance);
                    }
                }
                Collections.sort(stores);
            }
            return new CommonResult().success(stores);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }

    @IgnoreAuth
    @GetMapping(value = "/store/bestStore")
    @ApiOperation(value = "常去门店列表")
    public Object bestStore(@RequestParam(required = false)String longitude,
                            @RequestParam(required = false)String latitude) {
        try {
            List<QsgStore> stores = new ArrayList<>();
            UmsMember member = UserUtils.getCurrentMember();
            QueryWrapper qw = new QueryWrapper();
            qw.eq("member_id",member.getId());
            List<Integer> statues = new ArrayList<>();
            statues.add(OrderStatus.COMPLETED.getValue());
            statues.add(OrderStatus.HAS_PAYMONEY.getValue());
            qw.in("order_status",statues);
            qw.select("DISTINCT store_id");
            List<DmsOrder> orders = orderService.list(qw);
            if (null!=orders&&orders.size()>0){
                for (DmsOrder order : orders){
                    stores.add(storeService.getById(order.getStoreId()));
                }
            }
            Double fromLongitude = null;
            Double fromLatitude = null;
            if (StringUtils.isNotBlank(longitude)){
                fromLongitude = Double.valueOf(longitude);
                fromLatitude = Double.valueOf(latitude);
                if (null!=stores&&stores.size()>0){
                    for (QsgStore store : stores){
                        BigDecimal distance = DistanceUtils.getDistance(fromLongitude,fromLatitude,Double.valueOf(store.getLongitude()),Double.valueOf(store.getLatitude()),2);
                        store.setDistance(distance.toString());
                        store.setRealdistance(distance);
                    }
                }
                Collections.sort(stores);
            }
            return new CommonResult().success(stores);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult().failed();
        }
    }
}
